{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "from py_conv import batch_conv2d\n",
    "import numpy as np\n",
    "import torch.nn.functional as F\n",
    "import torch\n",
    "from time import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-4.193186151681344"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randn(3,4)\n",
    "b = np.random.randn(3,4)\n",
    "np.sum(a * b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "inputs = np.random.randn(32,3,28,28)\n",
    "weights = np.random.randn(8,3,5,5)\n",
    "stride = (1,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "start = time()\n",
    "for i in range(1874):\n",
    "    out_x = inputs.shape[2] - weights.shape[2] + 1\n",
    "    out_y = inputs.shape[3] - weights.shape[3] + 1\n",
    "    view_shape = (1, out_x, out_y, weights.shape[1], weights.shape[2], weights.shape[3])\n",
    "    _strides = inputs[0].data.strides*2\n",
    "    strided_batch = np.array([np.lib.stride_tricks.as_strided(x, view_shape, _strides) for x in inputs])[:, 0, 0:out_x:stride[0], 0:out_y:stride[1]]\n",
    "    result1 = batch_conv2d(strided_batch,weights, [32,8,24,24]).reshape([32,8,24,24])\n",
    "c_used = time() - start\n",
    "\n",
    "torch_inputs = torch.tensor(inputs)\n",
    "torch_weights = torch.tensor(weights)\n",
    "torch_bias = torch.zeros(8)\n",
    "start = time()\n",
    "for i in range(1874):\n",
    "    result2 = F.conv2d(torch_inputs, torch_weights, torch_bias)\n",
    "torch_used = time() - start"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C Function Used -> 34.220893144607544\n",
      "Pytorch Used    -> 5.736648797988892\n"
     ]
    }
   ],
   "source": [
    "print(f\"C Function Used -> {c_used}\")\n",
    "print(f\"Pytorch Used    -> {torch_used}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Assert Result\n",
    "np.testing.assert_almost_equal(result1, result2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "C Function Used -> 6.641327142715454  \n",
    "Pytorch Used    -> 1.343163251876831\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
